# 📸 获取4张接口测试截图 - 自动化辅助脚本
# 此脚本会执行4个API测试并详细显示结果，帮助你完成截图任务

# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "" 
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host "  📸 获取4张接口测试截图 - 自动化测试脚本" -ForegroundColor Cyan  
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host "" 

$BASE_URL = "http://localhost:3001/api"
$global:createdBookId = $null

# 辅助函数：美化显示JSON
function Show-Json {
    param($obj)
    $obj | ConvertTo-Json -Depth 10 | Write-Host -ForegroundColor White
}

# 辅助函数：显示分隔线
function Show-Separator {
    Write-Host "" 
    Write-Host "─────────────────────────────────────────────────────────" -ForegroundColor Gray
    Write-Host "" 
}

# 检查服务器是否运行
Write-Host "🔍 检查服务器状态..." -ForegroundColor Yellow
try {
    $healthCheck = Invoke-WebRequest -Uri "$BASE_URL/books" -Method Get -TimeoutSec 3 -ErrorAction Stop
    Write-Host "✅ 服务器运行正常！" -ForegroundColor Green
} catch {
    Write-Host "❌ 错误：无法连接到服务器！" -ForegroundColor Red
    Write-Host "" 
    Write-Host "请先启动后端服务器：" -ForegroundColor Yellow
    Write-Host "  cd backend" -ForegroundColor White
    Write-Host "  npm run dev" -ForegroundColor White
    Write-Host "" 
    exit 1
}

Show-Separator

#==============================================================================
# 📷 截图 1：查询所有图书（GET /api/books）
#==============================================================================

Write-Host "📷 截图 1：查询所有图书" -ForegroundColor Cyan
Write-Host "" 
Write-Host "请求信息：" -ForegroundColor Yellow
Write-Host "  方法: GET" -ForegroundColor White
Write-Host "  URL:  $BASE_URL/books?page=1&limit=10" -ForegroundColor White
Write-Host "" 

try {
    $response1 = Invoke-RestMethod -Uri "$BASE_URL/books?page=1&limit=10" -Method Get
    
    Write-Host "响应状态: 200 OK" -ForegroundColor Green
    Write-Host "" 
    Write-Host "响应数据：" -ForegroundColor Yellow
    Show-Json $response1
    
    Write-Host "" 
    Write-Host "📊 统计信息：" -ForegroundColor Cyan
    Write-Host "  共找到 $($response1.data.pagination.total) 本图书" -ForegroundColor White
    Write-Host "  当前页码：$($response1.data.pagination.page) / $($response1.data.pagination.pages)" -ForegroundColor White
    
    Write-Host "" 
    Write-Host "✅ 测试成功！" -ForegroundColor Green
    Write-Host "" 
    Write-Host "📸 Postman 截图步骤：" -ForegroundColor Yellow
    Write-Host "  1. 在 Postman 中选择 '1. 查询所有图书'" -ForegroundColor White
    Write-Host "  2. 点击 Send 按钮" -ForegroundColor White
    Write-Host "  3. 截图包含：请求方法、URL、响应状态、响应Body" -ForegroundColor White
    Write-Host "  4. 保存为：1-查询所有图书.png" -ForegroundColor White
    
} catch {
    Write-Host "❌ 测试失败：$($_.Exception.Message)" -ForegroundColor Red
}

Show-Separator
Write-Host "按任意键继续到 截图 2..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "" 

#==============================================================================
# 📷 截图 2：新增图书（POST /api/books）
#==============================================================================

Write-Host "📷 截图 2：新增图书" -ForegroundColor Cyan
Write-Host "" 
Write-Host "请求信息：" -ForegroundColor Yellow
Write-Host "  方法: POST" -ForegroundColor White
Write-Host "  URL:  $BASE_URL/books" -ForegroundColor White
Write-Host "" 

$newBookData = @{
    title = "Node.js 实战"
    author = "张三"
    publishDate = "2023-01-01"
    price = 99.99
    isbn = "9787111234567"
    publisher = "电子工业出版社"
    category = "编程技术"
    description = "深入浅出讲解 Node.js 开发技术"
    stock = 100
}

Write-Host "请求 Body：" -ForegroundColor Yellow
Show-Json $newBookData

$newBookJson = $newBookData | ConvertTo-Json

try {
    $response2 = Invoke-RestMethod -Uri "$BASE_URL/books" -Method Post -Body $newBookJson -ContentType "application/json"
    $global:createdBookId = $response2.data._id
    
    Write-Host "" 
    Write-Host "响应状态: 201 Created" -ForegroundColor Green
    Write-Host "" 
    Write-Host "响应数据：" -ForegroundColor Yellow
    Show-Json $response2
    
    Write-Host "" 
    Write-Host "📊 新增图书信息：" -ForegroundColor Cyan
    Write-Host "  图书 ID：$($response2.data._id)" -ForegroundColor White
    Write-Host "  书名：$($response2.data.title)" -ForegroundColor White
    Write-Host "  作者：$($response2.data.author)" -ForegroundColor White
    Write-Host "  价格：¥$($response2.data.price)" -ForegroundColor White
    
    Write-Host "" 
    Write-Host "⚠️  重要：请复制这个 ID，截图 3 和 4 会用到！" -ForegroundColor Red
    Write-Host "  Book ID: $global:createdBookId" -ForegroundColor Yellow
    
    # 保存到文件
    $global:createdBookId | Out-File -FilePath "book-id-for-screenshots.txt" -Encoding UTF8
    Write-Host "  (已自动保存到 book-id-for-screenshots.txt)" -ForegroundColor Gray
    
    Write-Host "" 
    Write-Host "✅ 测试成功！" -ForegroundColor Green
    Write-Host "" 
    Write-Host "📸 Postman 截图步骤：" -ForegroundColor Yellow
    Write-Host "  1. 在 Postman 中选择 '3. 新增图书'" -ForegroundColor White
    Write-Host "  2. 查看 Body 标签页的 JSON 数据" -ForegroundColor White
    Write-Host "  3. 点击 Send 按钮" -ForegroundColor White
    Write-Host "  4. 截图包含：请求方法、URL、请求Body、响应状态、响应Body" -ForegroundColor White
    Write-Host "  5. 保存为：2-新增图书.png" -ForegroundColor White
    
} catch {
    Write-Host "❌ 测试失败：$($_.Exception.Message)" -ForegroundColor Red
    Write-Host "提示：如果提示 ISBN 已存在，请修改 ISBN 值" -ForegroundColor Yellow
}

Show-Separator
Write-Host "按任意键继续到 截图 3..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "" 

#==============================================================================
# 📷 截图 3：查询单本图书（GET /api/books/:id）
#==============================================================================

Write-Host "📷 截图 3：查询单本图书" -ForegroundColor Cyan
Write-Host "" 

if (-not $global:createdBookId) {
    Write-Host "⚠️  警告：没有找到图书 ID" -ForegroundColor Yellow
    Write-Host "请先运行截图 2 创建图书" -ForegroundColor Yellow
} else {
    Write-Host "请求信息：" -ForegroundColor Yellow
    Write-Host "  方法: GET" -ForegroundColor White
    Write-Host "  URL:  $BASE_URL/books/$global:createdBookId" -ForegroundColor White
    Write-Host "" 
    
    try {
        $response3 = Invoke-RestMethod -Uri "$BASE_URL/books/$global:createdBookId" -Method Get
        
        Write-Host "响应状态: 200 OK" -ForegroundColor Green
        Write-Host "" 
        Write-Host "响应数据：" -ForegroundColor Yellow
        Show-Json $response3
        
        Write-Host "" 
        Write-Host "📊 图书详情：" -ForegroundColor Cyan
        Write-Host "  图书 ID：$($response3.data._id)" -ForegroundColor White
        Write-Host "  书名：$($response3.data.title)" -ForegroundColor White
        Write-Host "  作者：$($response3.data.author)" -ForegroundColor White
        Write-Host "  出版日期：$($response3.data.publishDate)" -ForegroundColor White
        Write-Host "  价格：¥$($response3.data.price)" -ForegroundColor White
        
        Write-Host "" 
        Write-Host "✅ 测试成功！" -ForegroundColor Green
        Write-Host "" 
        Write-Host "📸 Postman 截图步骤：" -ForegroundColor Yellow
        Write-Host "  1. 在 Postman 中选择 '2. 查询单本图书'" -ForegroundColor White
        Write-Host "  2. 将 URL 中的 :id 替换为：$global:createdBookId" -ForegroundColor White
        Write-Host "  3. 点击 Send 按钮" -ForegroundColor White
        Write-Host "  4. 截图包含：请求方法、URL（含ID）、响应状态、响应Body" -ForegroundColor White
        Write-Host "  5. 保存为：3-查询单本图书.png" -ForegroundColor White
        
    } catch {
        Write-Host "❌ 测试失败：$($_.Exception.Message)" -ForegroundColor Red
    }
}

Show-Separator
Write-Host "按任意键继续到 截图 4..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "" 

#==============================================================================
# 📷 截图 4：删除图书（DELETE /api/books/:id）
#==============================================================================

Write-Host "📷 截图 4：删除图书" -ForegroundColor Cyan
Write-Host "" 

if (-not $global:createdBookId) {
    Write-Host "⚠️  警告：没有找到图书 ID" -ForegroundColor Yellow
    Write-Host "请先运行截图 2 创建图书" -ForegroundColor Yellow
} else {
    Write-Host "请求信息：" -ForegroundColor Yellow
    Write-Host "  方法: DELETE" -ForegroundColor White
    Write-Host "  URL:  $BASE_URL/books/$global:createdBookId" -ForegroundColor White
    Write-Host "" 
    
    try {
        $response4 = Invoke-RestMethod -Uri "$BASE_URL/books/$global:createdBookId" -Method Delete
        
        Write-Host "响应状态: 200 OK" -ForegroundColor Green
        Write-Host "" 
        Write-Host "响应数据：" -ForegroundColor Yellow
        Show-Json $response4
        
        Write-Host "" 
        Write-Host "📊 删除结果：" -ForegroundColor Cyan
        Write-Host "  已删除图书 ID：$($response4.data._id)" -ForegroundColor White
        Write-Host "  书名：$($response4.data.title)" -ForegroundColor White
        
        Write-Host "" 
        Write-Host "✅ 测试成功！" -ForegroundColor Green
        Write-Host "" 
        Write-Host "📸 Postman 截图步骤：" -ForegroundColor Yellow
        Write-Host "  1. 在 Postman 中选择 '5. 删除图书'" -ForegroundColor White
        Write-Host "  2. 将 URL 中的 :id 替换为：$global:createdBookId" -ForegroundColor White
        Write-Host "  3. 点击 Send 按钮" -ForegroundColor White
        Write-Host "  4. 截图包含：请求方法、URL（含ID）、响应状态、响应Body" -ForegroundColor White
        Write-Host "  5. 保存为：4-删除图书.png" -ForegroundColor White
        
    } catch {
        Write-Host "❌ 测试失败：$($_.Exception.Message)" -ForegroundColor Red
    }
}

Show-Separator

#==============================================================================
# 总结
#==============================================================================

Write-Host "" 
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host "  ✅ 所有测试完成！" -ForegroundColor Cyan
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host "" 

Write-Host "📋 接下来的步骤：" -ForegroundColor Yellow
Write-Host "" 
Write-Host "1️⃣  打开 Postman" -ForegroundColor White
Write-Host "2️⃣  导入文件：postman_collection.json" -ForegroundColor White
Write-Host "3️⃣  按照上面的提示，依次完成 4 张截图" -ForegroundColor White
Write-Host "4️⃣  确保截图清晰，包含所有必要信息" -ForegroundColor White
Write-Host "" 

Write-Host "📁 截图命名要求：" -ForegroundColor Yellow
Write-Host "  ✅ 1-查询所有图书.png" -ForegroundColor White
Write-Host "  ✅ 2-新增图书.png" -ForegroundColor White
Write-Host "  ✅ 3-查询单本图书.png" -ForegroundColor White
Write-Host "  ✅ 4-删除图书.png" -ForegroundColor White
Write-Host "" 

if ($global:createdBookId) {
    Write-Host "💡 提示：Book ID 已保存到文件" -ForegroundColor Cyan
    Write-Host "  文件位置：book-id-for-screenshots.txt" -ForegroundColor White
    Write-Host "  Book ID：$global:createdBookId" -ForegroundColor Yellow
}

Write-Host "" 
Write-Host "📚 更多帮助，请查看：" -ForegroundColor Cyan
Write-Host "  - 获取截图步骤.md" -ForegroundColor White
Write-Host "  - HOW_TO_GET_TEST_SCREENSHOTS.md" -ForegroundColor White
Write-Host "" 
Write-Host "🎉 祝你顺利完成截图任务！" -ForegroundColor Green
Write-Host ""
